package com.jn.glide.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.jn.glide.R;
import com.jn.glide.entity.AvatarEntity;
import com.jn.glide.views.OKHttpPicassoActivity;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

public class TitlesAdapter extends RecyclerView.Adapter<TitlesAdapter.ViewHolder> {
    private List<OKHttpPicassoActivity.Entity.Data> mList;
    private OnItemClickListener mListener;

    public TitlesAdapter(List<OKHttpPicassoActivity.Entity.Data> list) {
        mList = list;
    }

    public void updateData(List<OKHttpPicassoActivity.Entity.Data> list) {
        mList.clear();
        mList = list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public TitlesAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.tittles_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull TitlesAdapter.ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        OKHttpPicassoActivity.Entity.Data resultDTO = mList.get(position);
        holder.textView1.setText(resultDTO.title);
        Picasso.get()
                .load(resultDTO.url)
                .into(holder.image);

        holder.textView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onItemClick(resultDTO);
                }
            }
        });
        holder.image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onItemClick(resultDTO);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView textView1;
        ImageView image;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            image = itemView.findViewById(R.id.image);
            textView1 = itemView.findViewById(R.id.name);
        }
    }

    public void addItemClickListener(OnItemClickListener listener) {
        mListener = listener;
    }

    public interface OnItemClickListener {
        void onItemClick(OKHttpPicassoActivity.Entity.Data ite);
    }
}
